﻿package mortal.game.view.common.menu
{
    import com.gengine.global.*;
    import fl.data.*;
    import fl.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.game.manager.*;

    public class PlayerMenuRegister extends Object
    {
        private static var _playerOpList:ListMenu;
        private static var _layer:Sprite;
        private static var _currnetActiveObject:InteractiveObject;
        private static var _currentPlayerData:Object = null;
        private static var _data:Object = null;
        private static var _dicObjInfo:Dictionary = new Dictionary(true);
        private static var _dicObjdata:Dictionary = new Dictionary(true);

        public function PlayerMenuRegister()
        {
            return;
        }// end function

        public static function Register(param1:InteractiveObject, param2, param3:DataProvider = null) : void
        {
            if (!HasRegister(param1))
            {
                if (!param3)
                {
                    param3 = PlayerMenuConst.ChatOpMenu;
                }
                if (param3.length == 0)
                {
                    return;
                }
                _dicObjInfo[param1] = param2;
                _dicObjdata[param1] = param3;
                param1.addEventListener(MouseEvent.CLICK, showInfo, false, 0);
            }
            return;
        }// end function

        private static function unRegister(event:Event) : void
        {
            PlayerMenuRegister.UnRegister(event.target as InteractiveObject);
            return;
        }// end function

        public static function UnRegister(param1:InteractiveObject) : void
        {
            param1.removeEventListener(MouseEvent.CLICK, showInfo);
            if (_dicObjInfo[param1])
            {
                delete _dicObjInfo[param1];
                delete _dicObjdata[param1];
            }
            return;
        }// end function

        public static function ShowOpList(param1, param2:Number, param3:Number, param4:DataProvider = null) : void
        {
            if (!param4)
            {
                param4 = PlayerMenuConst.ChatOpMenu;
            }
            if (!_layer)
            {
                _layer = LayerManager.topLayer;
            }
            createPlayerOpList();
            _currentPlayerData = param1;
            _layer.addChild(_playerOpList);
            _playerOpList.dataProvider = PlayerMenuConst.getEnabeldAttri(param4, param1);
            param2 = param2 < _layer.stage.stageWidth - _playerOpList.width ? (param2) : (param2 - _playerOpList.width);
            param3 = param3 < _layer.stage.stageHeight - _playerOpList.height ? (param3) : (param3 - _playerOpList.height);
            _playerOpList.show(param2, param3, _layer);
            return;
        }// end function

        public static function HasRegister(param1:InteractiveObject) : Boolean
        {
            return _dicObjInfo[param1] ? (true) : (false);
        }// end function

        public static function HideOpList() : void
        {
            if (_playerOpList)
            {
                _playerOpList.hide();
            }
            return;
        }// end function

        private static function createPlayerOpList() : void
        {
            if (!_playerOpList)
            {
                _playerOpList = new ListMenu();
                _playerOpList.width = 86;
                _playerOpList.visible = false;
                _playerOpList.list.setStyle("cellRenderer", PlayerMenuCellRenderer);
                _playerOpList.list.drawNow();
                _playerOpList.list.addEventListener(ListEvent.ITEM_CLICK, PlayerOpItemSelect);
            }
            return;
        }// end function

        public static function showInfo(event:MouseEvent) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            if (!_layer)
            {
                _layer = LayerManager.topLayer;
            }
            createPlayerOpList();
            _currnetActiveObject = event.currentTarget as InteractiveObject;
            _currentPlayerData = _dicObjInfo[_currnetActiveObject];
            if (_currnetActiveObject.parent)
            {
                _layer.addChild(_playerOpList);
                _playerOpList.dataProvider = PlayerMenuConst.getEnabeldAttri(_dicObjdata[_currnetActiveObject], _dicObjInfo[_currnetActiveObject]);
                _loc_2 = event.stageX;
                _loc_3 = event.stageY;
                _loc_2 = _loc_2 < _layer.stage.stageWidth - _playerOpList.width ? (_loc_2) : (_loc_2 - _playerOpList.width);
                _loc_3 = _loc_3 < _layer.stage.stageHeight - _playerOpList.height ? (_loc_3) : (_loc_3 - _playerOpList.height);
                _playerOpList.show(_loc_2, _loc_3, _layer);
            }
            Global.stage.addEventListener(MouseEvent.CLICK, hideHandler);
            event.stopImmediatePropagation();
            return;
        }// end function

        private static function hideHandler(event:MouseEvent) : void
        {
            HideOpList();
            Global.stage.removeEventListener(MouseEvent.CLICK, hideHandler);
            return;
        }// end function

        private static function PlayerOpItemSelect(event:ListEvent) : void
        {
            PlayerMenuConst.Opearte(_playerOpList.list.dataProvider.getItemAt(event.index)["label"], _currentPlayerData);
            return;
        }// end function

    }
}
